package com.ruoyi.puyu.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.puyu.domain.PuyuMemberIntegral;
import com.ruoyi.puyu.service.IPuyuMemberIntegralService;
import com.ruoyi.puyu.service.MemberIntegralService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @description: 会员积分服务实现
 * @author: Marvin
 * @create: 2025-01-06 00:00
 */
@Service
public class MemberIntegralServiceImpl implements MemberIntegralService {
    private static final Logger logger = LoggerFactory.getLogger(MemberIntegralServiceImpl.class);
//
//    @Autowired
//    public RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private IPuyuMemberIntegralService puyuMemberIntegralService;
    @Autowired
    private RedisCache redisCache;

    private static final String MEMBER_INTEGRAL_KEY = "member:integral";
    @PostConstruct
    public void init() {
        try {
            boolean exists = redisCache.hasKey(MEMBER_INTEGRAL_KEY);
            if (!exists) {
                List<PuyuMemberIntegral> memberIntegrals = puyuMemberIntegralService.selectPuyuMemberIntegralList(new PuyuMemberIntegral());
                for (PuyuMemberIntegral memberIntegral : memberIntegrals) {
                    String memberId = memberIntegral.getMemberId() + "_" + memberIntegral.getMemberName();
                    redisCache.addMemberIntegral(memberId, memberIntegral.getIntegralTotal());
                }
            }
        } catch (Exception e) {
            // 记录日志并处理异常
            logger.error("Failed to initialize member integral: {}", e.getMessage());
            throw new RuntimeException("Failed to initialize member integral", e);
        }
    }
//
//    @Override
//    public void addMemberIntegral(String memberId, Long integral) {
//        try {
//            redisTemplate.opsForZSet().add(MEMBER_INTEGRAL_KEY, memberId, integral);
//        } catch (Exception e) {
//            // 记录日志并处理异常
//            logger.error("Failed to add member integral: {}", e.getMessage());
//            throw new RuntimeException("Failed to add member integral", e);
//        }
//    }
//
//    @Override
//    public Long getMemberIntegral(String memberId) {
//        try {
//            Double score = redisTemplate.opsForZSet().score(MEMBER_INTEGRAL_KEY, memberId);
//            return score != null ? score.longValue() : 0L;
//        } catch (Exception e) {
//            // 记录日志并处理异常
//            logger.error("Failed to get member integral: {}", e.getMessage());
//            throw new RuntimeException("Failed to get member integral", e);
//        }
//    }
//
//    @Override
//    public Long getMemberRank(String memberId) {
//        try {
//            return redisTemplate.opsForZSet().reverseRank(MEMBER_INTEGRAL_KEY, memberId);
//        } catch (Exception e) {
//            // 记录日志并处理异常
//            logger.error("Failed to get member rank: {}", e.getMessage());
//            throw new RuntimeException("Failed to get member rank", e);
//        }
//    }
//
//    @Override
//    public Set<Object> getTopMembers(int start, int end) {
//        if (start < 0 || end < start) {
//            throw new IllegalArgumentException("Invalid range parameters");
//        }
//        try {
//            return Collections.singleton(redisTemplate.opsForZSet().reverseRangeWithScores(MEMBER_INTEGRAL_KEY, start, end));
//        } catch (Exception e) {
//            // 记录日志并处理异常
//            logger.error("Failed to get top members: {}", e.getMessage());
//            throw new RuntimeException("Failed to get top members", e);
//        }
//    }
//
//    @Override
//    public Map<String, Object> getMemberRankAndScore(String memberId) {
//        try {
//            Double score = redisTemplate.opsForZSet().score(MEMBER_INTEGRAL_KEY, memberId);
//            Long rank = redisTemplate.opsForZSet().reverseRank(MEMBER_INTEGRAL_KEY, memberId);
//            Map<String, Object> result = new HashMap<>();
//            result.put("rank", rank);
//            result.put("score", score != null ? score.longValue() : 0L);
//            return result;
//        } catch (Exception e) {
//            // 记录日志并处理异常
//            logger.error("Failed to get member rank and score: {}", e.getMessage());
//            throw new RuntimeException("Failed to get member rank and score", e);
//        }
//    }
}
